#!/usr/bin/env bash

DEBUGMODE=${DEBUGMODE:-default}
DOCKER_IMAGE_VERSION=${DOCKER_IMAGE_VERSION:-1.0.0}
DOCKER_IMAGE_BASENAME=${DOCKER_IMAGE_BASENAME:-bizark_epic}
DOMAIN=${DOMAIN:-thebizark.com}
DOCKER_REGISTRY_USERNAME=${DOCKER_REGISTRY_USERNAME:-bizark}
DOCKER_REGISTRY_HOST=${DOCKER_REGISTRY_HOST:-docker.bizarkdev.com}
DOCKER_REGISTRY_REPOSITORY=${DOCKER_REGISTRY_REPOSITORY:-thebizark}

PRJ_PATH="$(cd "$(dirname "${BASH_SOURCE[0]}")"/ >/dev/null && pwd)"
PRJ_M2_PATH=${PRJ_PATH}/.mvn/settings
PRJ_CERTS_PATH=${PRJ_PATH}/.certs
DATA_PATH=${PRJ_PATH}/data
DATA_TPL_PATH=${PRJ_PATH}/extra/data_template
DOT_DOCKER_PATH=${HOME}/.docker
DOT_DOCKER_DATA_PATH=${DOT_DOCKER_PATH}/data
DATA_PATH_HOST=${DOT_DOCKER_DATA_PATH}
CODE_PATH=${HOME}/Code
CODE_DATA_PATH=${CODE_PATH}/data
DOCKER_BUILDKIT=1
BUILDKIT_STEP_LOG_MAX_SIZE=-1
BUILDKIT_STEP_LOG_MAX_SPEED=-1

DOCKERPW_CONF_FILEPATH=${PRJ_PATH}/dockerpw.conf
if [ -f ${DOCKERPW_CONF_FILEPATH} ]; then
  . ${DOCKERPW_CONF_FILEPATH}
fi

ACTION="${1:-help}"
APP_MOD="all"
if [[ "${ACTION}" == "download" || "${ACTION}" == "run" || "${ACTION}" == "up" || "${ACTION}" == "daemon" || "${ACTION}" == "logs" || "${ACTION}" == "sh" || "${ACTION}" == "stop" ]]; then
  APP_MOD="${2:-all}"
  APP_ENV="${3:-dev}"
  if [ $# -gt 3 ]; then
    DEBUGMODE="${4:-default}"
  fi
else
  APP_ENV="${2:-dev}"
  if [ $# -gt 2 ]; then
    DEBUGMODE="${3:-default}"
  fi
fi
if [[ ${APP_ENV} == prod ]]; then
  APP_VER=latest
else
  APP_VER=${APP_ENV}
fi

echo ""
echo "DOCKER_IMAGE_BASENAME: ${DOCKER_IMAGE_BASENAME}"
echo "DOCKER_IMAGE_VERSION: ${DOCKER_IMAGE_VERSION}"
echo "action: ${ACTION}"
echo "app mod: ${APP_MOD}"
echo "app env: ${APP_ENV}"
echo "app version: ${APP_VER}"
echo "debug mode: ${DEBUGMODE}"
echo ""

#exit 1;

case ${ACTION} in
login)
  docker login --username=${DOCKER_REGISTRY_USERNAME} ${DOCKER_REGISTRY_HOST}
  ;;
prepare)

  if [ ! -d ${DOT_DOCKER_PATH} ]; then
    mkdir -p ${DOT_DOCKER_PATH}
  fi
  if [ ! -d ${DOT_DOCKER_DATA_PATH} ]; then
    mkdir -p ${DOT_DOCKER_DATA_PATH}
  fi
  if [ ! -d ${DATA_PATH} ]; then
    ln -nfs ${DATA_PATH_HOST} ${DATA_PATH}
  fi
  if [ -d ${CODE_PATH} ]; then
    if [ ! -d ${CODE_DATA_PATH} ]; then
      ln -nfs ${DATA_PATH_HOST} ${CODE_DATA_PATH}
    fi
  fi

  if [ -d ${DATA_TPL_PATH} ]; then
    rsync -ravzh --progress --stats --delay-updates --exclude=".git/" --exclude="*.log" "${DATA_TPL_PATH%/}/" "${DATA_PATH%/}/"
  fi

#  if [ ! -d ${PRJ_M2_PATH} ]; then
#    mkdir -p ${PRJ_M2_PATH}
#  fi
#
#  if [ ! -e ${PRJ_M2_PATH}/settings.xml ]; then
#    if [ -e ${HOME}/.m2/settings.xml ]; then
#      cp -af ${HOME}/.m2/settings.xml ${PRJ_M2_PATH}/
#    fi
#  fi


  if [ ! -d ${PRJ_CERTS_PATH} ]; then
    mkdir -p ${PRJ_CERTS_PATH}
  fi
  if [ ! -d ${PRJ_CERTS_PATH}/${DOMAIN} ]; then
    mkdir -p ${PRJ_CERTS_PATH}/${DOMAIN}
  fi
  PRJ_DATA_DOMAIN_PATH=${DATA_PATH}/etc/ssl/certs/${DOMAIN}
  PRJ_CERT_DOMAIN_PATH=${PRJ_CERTS_PATH}/${DOMAIN}
  if [ -d ${PRJ_CERT_DOMAIN_PATH} ]; then
    rsync -ravzh --progress --stats --delay-updates --exclude=".git/" --exclude="*.log" "${PRJ_DATA_DOMAIN_PATH%/}/" "${PRJ_CERT_DOMAIN_PATH%/}/"
  fi
  ;;
setupbuildx)
    DOCKER_BUILDKIT=1 docker buildx rm mybuild
    DOCKER_BUILDKIT=1 docker buildx create --driver-opt env.BUILDKIT_STEP_LOG_MAX_SIZE=1000000,env.BUILDKIT_STEP_LOG_MAX_SPEED=100000000 --use --node mybuild --name mybuild
    DOCKER_BUILDKIT=1 docker buildx use mybuild
  ;;
package)
  cd ${PRJ_PATH}
  npm i
  if [[ "${APP_ENV}" == "prod" ]]; then
      npm run build
  else
      npm run build:${APP_ENV}
  fi
  ;;
build)
  cd ${PRJ_PATH}
  if [[ "${DEBUGMODE}" == "debug" ]]; then
      DOCKER_BUILDKIT=1 docker build -m 3g --memory-swap=-1 -f ${DEBUGMODE}.Dockerfile -t ${DOCKER_REGISTRY_HOST}/${DOCKER_REGISTRY_REPOSITORY}/${DOCKER_IMAGE_BASENAME}:${DEBUGMODE} --build-arg SPRING_PROFILES_ACTIVE=${APP_ENV} .
  else
    if [[ "${APP_ENV}" == "prod" ]]; then
      DOCKER_BUILDKIT=1 docker build -m 3g --memory-swap=-1 -f ${DEBUGMODE}.Dockerfile -t ${DOCKER_REGISTRY_HOST}/${DOCKER_REGISTRY_REPOSITORY}/${DOCKER_IMAGE_BASENAME}:${APP_VER} -t ${DOCKER_REGISTRY_HOST}/${DOCKER_REGISTRY_REPOSITORY}/${DOCKER_IMAGE_BASENAME}:${DOCKER_IMAGE_VERSION} --build-arg SPRING_PROFILES_ACTIVE=${APP_ENV} .
    else
      DOCKER_BUILDKIT=1 docker build -m 3g --memory-swap=-1 -f ${DEBUGMODE}.Dockerfile -t ${DOCKER_REGISTRY_HOST}/${DOCKER_REGISTRY_REPOSITORY}/${DOCKER_IMAGE_BASENAME}:${APP_VER} --build-arg SPRING_PROFILES_ACTIVE=${APP_ENV} .
    fi
  fi
  ;;
buildnocache)
  cd ${PRJ_PATH}
  if [[ "${DEBUGMODE}" == "debug" ]]; then
      DOCKER_BUILDKIT=1 docker build -m 3g --memory-swap=-1 --no-cache -f ${DEBUGMODE}.Dockerfile -t ${DOCKER_REGISTRY_HOST}/${DOCKER_REGISTRY_REPOSITORY}/${DOCKER_IMAGE_BASENAME}:${DEBUGMODE} --build-arg SPRING_PROFILES_ACTIVE=${APP_ENV} .
  else
    if [[ "${APP_ENV}" == "prod" ]]; then
      DOCKER_BUILDKIT=1 docker build -m 3g --memory-swap=-1 --no-cache -f ${DEBUGMODE}.Dockerfile -t ${DOCKER_REGISTRY_HOST}/${DOCKER_REGISTRY_REPOSITORY}/${DOCKER_IMAGE_BASENAME}:${APP_VER} -t ${DOCKER_REGISTRY_HOST}/${DOCKER_REGISTRY_REPOSITORY}/${DOCKER_IMAGE_BASENAME}:${DOCKER_IMAGE_VERSION} --build-arg SPRING_PROFILES_ACTIVE=${APP_ENV} .
    else
      DOCKER_BUILDKIT=1 docker build -m 3g --memory-swap=-1 --no-cache -f ${DEBUGMODE}.Dockerfile -t ${DOCKER_REGISTRY_HOST}/${DOCKER_REGISTRY_REPOSITORY}/${DOCKER_IMAGE_BASENAME}:${APP_VER} --build-arg SPRING_PROFILES_ACTIVE=${APP_ENV} .
    fi
  fi


  ;;
buildx)
  cd ${PRJ_PATH}
  if [[ "${DEBUGMODE}" == "debug" ]]; then
    DOCKER_BUILDKIT=1 docker buildx build -m 3g --memory-swap=-1 -f ${DEBUGMODE}.Dockerfile -t ${DOCKER_REGISTRY_HOST}/${DOCKER_REGISTRY_REPOSITORY}/${DOCKER_IMAGE_BASENAME}:${DEBUGMODE} --build-arg SPRING_PROFILES_ACTIVE=${APP_ENV} .
  else
    if [[ "${APP_ENV}" == "prod" ]]; then
      DOCKER_BUILDKIT=1 docker buildx build -m 3g --memory-swap=-1 -f ${DEBUGMODE}.Dockerfile -t ${DOCKER_REGISTRY_HOST}/${DOCKER_REGISTRY_REPOSITORY}/${DOCKER_IMAGE_BASENAME}:${APP_VER} -t ${DOCKER_REGISTRY_HOST}/${DOCKER_REGISTRY_REPOSITORY}/${DOCKER_IMAGE_BASENAME}:${DOCKER_IMAGE_VERSION} --build-arg SPRING_PROFILES_ACTIVE=${APP_ENV} .
    else
      DOCKER_BUILDKIT=1 docker buildx build -m 3g --memory-swap=-1 -f ${DEBUGMODE}.Dockerfile -t ${DOCKER_REGISTRY_HOST}/${DOCKER_REGISTRY_REPOSITORY}/${DOCKER_IMAGE_BASENAME}:${APP_VER} --build-arg SPRING_PROFILES_ACTIVE=${APP_ENV} .
    fi
  fi
  ;;
buildxnocache)
  cd ${PRJ_PATH}
  if [[ "${DEBUGMODE}" == "debug" ]]; then
    DOCKER_BUILDKIT=1 docker buildx build -m 3g --memory-swap=-1 --no-cache -f ${DEBUGMODE}.Dockerfile -t ${DOCKER_REGISTRY_HOST}/${DOCKER_REGISTRY_REPOSITORY}/${DOCKER_IMAGE_BASENAME}:${DEBUGMODE} --build-arg SPRING_PROFILES_ACTIVE=${APP_ENV} .
  else
    if [[ "${APP_ENV}" == "prod" ]]; then
      DOCKER_BUILDKIT=1 docker buildx build -m 3g --memory-swap=-1 --no-cache -f ${DEBUGMODE}.Dockerfile -t ${DOCKER_REGISTRY_HOST}/${DOCKER_REGISTRY_REPOSITORY}/${DOCKER_IMAGE_BASENAME}:${APP_VER} -t ${DOCKER_REGISTRY_HOST}/${DOCKER_REGISTRY_REPOSITORY}/${DOCKER_IMAGE_BASENAME}:${DOCKER_IMAGE_VERSION} --build-arg SPRING_PROFILES_ACTIVE=${APP_ENV} .
    else
      DOCKER_BUILDKIT=1 docker buildx build -m 3g --memory-swap=-1 --no-cache -f ${DEBUGMODE}.Dockerfile -t ${DOCKER_REGISTRY_HOST}/${DOCKER_REGISTRY_REPOSITORY}/${DOCKER_IMAGE_BASENAME}:${APP_VER} --build-arg SPRING_PROFILES_ACTIVE=${APP_ENV} .
    fi
  fi
  ;;
up)
  cd ${PRJ_PATH}
  if [[ "${APP_MOD}" == "all" ]]; then
    if [[ "${DEBUGMODE}" == "debug" ]]; then
      docker-compose -p ${DOCKER_IMAGE_BASENAME} -f docker-compose.debug.yml up --no-build
    else
      docker-compose -p ${DOCKER_IMAGE_BASENAME} up --no-build
    fi
  else
    if [[ "${DEBUGMODE}" == "debug" ]]; then
      docker-compose -p ${DOCKER_IMAGE_BASENAME} -f docker-compose.debug.yml up --no-build ${DOCKER_IMAGE_BASENAME}_${APP_MOD}
    else
      docker-compose -p ${DOCKER_IMAGE_BASENAME} up --no-build ${DOCKER_IMAGE_BASENAME}_${APP_MOD}
    fi
  fi
  ;;
run)
  if [[ "${APP_MOD}" == "all" ]]; then
    docker-compose -p ${DOCKER_IMAGE_BASENAME} run ${DOCKER_IMAGE_BASENAME}
  else
    docker-compose -p ${DOCKER_IMAGE_BASENAME} run ${DOCKER_IMAGE_BASENAME}_${APP_MOD}
  fi
  ;;
daemon)
  cd ${PRJ_PATH}
  if [[ "${APP_MOD}" == "all" ]]; then
    if [[ "${DEBUGMODE}" == "debug" ]]; then
      docker-compose -p ${DOCKER_IMAGE_BASENAME} -f docker-compose.debug.yml up --no-build -d
    else
      docker-compose -p ${DOCKER_IMAGE_BASENAME} up --no-build -d
    fi
  else
    if [[ "${DEBUGMODE}" == "debug" ]]; then
      docker-compose -p ${DOCKER_IMAGE_BASENAME} -f docker-compose.debug.yml up --no-build -d ${DOCKER_IMAGE_BASENAME}_${APP_MOD}
    else
      docker-compose -p ${DOCKER_IMAGE_BASENAME} up --no-build -d ${DOCKER_IMAGE_BASENAME}_${APP_MOD}
    fi
  fi
  ;;
logs)
  if [[ "${APP_MOD}" == "all" ]]; then
    docker-compose -p ${DOCKER_IMAGE_BASENAME} logs -tf --tail=1000
  else
    docker-compose -p ${DOCKER_IMAGE_BASENAME} logs -tf --tail=1000 ${DOCKER_IMAGE_BASENAME}_${APP_MOD}
  fi
  ;;
sh)
  if [[ "${DEBUGMODE}" == "debug" ]]; then
    if [[ "${APP_MOD}" == "all" ]]; then
      docker-compose -p ${DOCKER_IMAGE_BASENAME} -f docker-compose.debug.yml exec -u www ${DOCKER_IMAGE_BASENAME} /bin/bash
    else
      docker-compose -p ${DOCKER_IMAGE_BASENAME} -f docker-compose.debug.yml exec -u www ${DOCKER_IMAGE_BASENAME}_${APP_MOD} /bin/bash
    fi
  else
      if [[ "${APP_MOD}" == "all" ]]; then
        docker-compose -p ${DOCKER_IMAGE_BASENAME} exec -u nginx ${DOCKER_IMAGE_BASENAME} /bin/bash
      else
        docker-compose -p ${DOCKER_IMAGE_BASENAME} exec -u nginx ${DOCKER_IMAGE_BASENAME}_${APP_MOD} /bin/bash
      fi
  fi
  ;;
stop)
  if [[ "${DEBUGMODE}" == "debug" ]]; then
    if [[ "${APP_MOD}" == "all" ]]; then
      docker-compose -p ${DOCKER_IMAGE_BASENAME} -f docker-compose.debug.yml stop -t 60
    else
      docker-compose -p ${DOCKER_IMAGE_BASENAME} -f docker-compose.debug.yml stop -t 60 ${DOCKER_IMAGE_BASENAME}_${APP_MOD}
    fi
  else
    if [[ "${APP_MOD}" == "all" ]]; then
      docker-compose -p ${DOCKER_IMAGE_BASENAME} stop -t 60
    else
      docker-compose -p ${DOCKER_IMAGE_BASENAME} stop -t 60 ${DOCKER_IMAGE_BASENAME}_${APP_MOD}
    fi
  fi
  ;;
down)
  if [[ "${DEBUGMODE}" == "debug" ]]; then
    docker-compose -p ${DOCKER_IMAGE_BASENAME} -f docker-compose.debug.yml down
  else
    docker-compose -p ${DOCKER_IMAGE_BASENAME} down
  fi
  ;;
push)
  if [[ "${DEBUGMODE}" == "debug" ]]; then
    docker push ${DOCKER_REGISTRY_HOST}/${DOCKER_REGISTRY_REPOSITORY}/${DOCKER_IMAGE_BASENAME}:${DEBUGMODE}
  else
    if [[ "${APP_ENV}" == "prod" ]]; then
      docker push ${DOCKER_REGISTRY_HOST}/${DOCKER_REGISTRY_REPOSITORY}/${DOCKER_IMAGE_BASENAME}:${APP_VER}
      docker push ${DOCKER_REGISTRY_HOST}/${DOCKER_REGISTRY_REPOSITORY}/${DOCKER_IMAGE_BASENAME}:${DOCKER_IMAGE_VERSION}
    else
      docker push ${DOCKER_REGISTRY_HOST}/${DOCKER_REGISTRY_REPOSITORY}/${DOCKER_IMAGE_BASENAME}:${APP_VER}
    fi
  fi
  ;;
pull)
  if [[ "${DEBUGMODE}" == "debug" ]]; then
    docker pull ${DOCKER_REGISTRY_HOST}/${DOCKER_REGISTRY_REPOSITORY}/${DOCKER_IMAGE_BASENAME}:${DEBUGMODE}
  else
    docker pull ${DOCKER_REGISTRY_HOST}/${DOCKER_REGISTRY_REPOSITORY}/${DOCKER_IMAGE_BASENAME}:${APP_VER}
  fi
  ;;
download)
  imageID="${DOCKER_REGISTRY_HOST}/${DOCKER_REGISTRY_REPOSITORY}/${DOCKER_IMAGE_BASENAME}:${APP_VER}"
  if [[ "${DEBUGMODE}" == "debug" ]]; then
    imageID="${DOCKER_REGISTRY_HOST}/${DOCKER_REGISTRY_REPOSITORY}/${DOCKER_IMAGE_BASENAME}:${DEBUGMODE}"
  fi
  containerID=$(docker create ${imageID})
  echo "${containerID}"
  docker cp ${containerID}:/home/www/Code/${DOCKER_IMAGE_BASENAME}/current/dist .
  docker rm -v ${containerID}
  echo "done"
  ;;
help);&
*)
  echo "USAGE ARGS: [action] [env] [debugmode]"
  echo "actions: prepare package build up run logs sh stop login push pull daemon download"
  echo "envs:"
  echo "dev"
  echo "test"
  echo "stage"
  echo "prod"
  echo "debugmode:"
  echo "debug"
  echo "default"
  echo ""
  echo "example usages:"
  echo "build dev debug"
  echo "build test"
  echo "up all prod default"
  echo "login"
  echo ""
  echo "workflows:"
  echo "[Local Build]"
  echo "login (run once)"
  echo "prepare  (run once)"
  echo "package (optional)"
  echo "build"
  echo "up (check status,errors)"
  echo "logs (check status,errors)"
  echo "sh (check status,errors)"
  echo "stop"
  echo "push (push to server)"
  echo ""
  echo "[Remote Run]"
  echo "login (run once)"
  echo "pull"
  echo "daemon"
  echo ""
  ;;
esac
